//这里要获取X轴滚动条位置
function getPageScroll() {
    var x ;
    if (window.pageYOffset) {    // all except IE
        x = window.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollLeft) {    // IE 6 Strict
        x = document.documentElement.scrollLeft;
    } else if (document.body) {    // all other IE
        x = document.body.scrollLeft;
    }
    return x;
}
//这里要获取Y轴滚动条位置
function getPageScrollY() {
    var  y;
    if (window.pageYOffset) {    // all except IE
        y = window.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {    // IE 6 Strict
        y = document.documentElement.scrollTop;
    } else if (document.body) {    // all other IE
        y = document.body.scrollTop;
    }
    return y;
}


function clearData() {//清除选框，包括选中的数据
    $("#selectDiv").remove();
    $("#tbgsbutton").remove();
    chooseArr = [];
}

var chooseArr = [];//这里是最终的选择的数据
var clickDiv = null ;//单次点击的div
$(function () {
    $("[name='choose']").on("click", function () {

        var thisSelDiv = $("#selectDiv");
        if(thisSelDiv!=null && thisSelDiv.html()!=null){
            clearData();
            clickDiv = null;
        }
        var selDiv2 = document.createElement("div");
        selDiv2.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;";
        selDiv2.id = "selectDiv";
        document.body.appendChild(selDiv2);
        //计算所有的格子，哪个格子，在这个点里。
        var l001 = $(this).offset().left;
        var t001 = $(this).offset().top;
        selDiv2.style.left = l001 +"px";
        selDiv2.style.top = t001+"px";
        selDiv2.style.width = "59px";
        selDiv2.style.height ="19px";
        var bb= $("#tbgsbutton");
        if(bb==null || bb.attr("id")==null){
            $("body").append("<img onclick='showTbgs()' src='../../ml/plug_in/images/bjbutton.png' id='tbgsbutton' " +
                "style='position:absolute;" +
                "top: "+t001+"px;" +
                "left: "+(l001-25)+"px;" +
                "width: 22px;height: 22px;z-index:1000;cursor: pointer;' />");
            clickDiv = $(this)[0];//单个选中的数据
        }
        var  evt = window.event || arguments[0];
        clearEventBubble(evt);

    });
});

(function () {


    //指定范围内 onmousedown , (  document 是 对整个页面)
    var optAreaDiv = document.getElementById("optAreaDiv");
    optAreaDiv.onmousedown = function () {

        var evt = window.event || arguments[0];
        if(evt.button != 0){//只让 数遍左键 起作用 0 左 1 中 2 右
            return;
        }

        var selectDivExists = $("#selectDiv");
        if(selectDivExists && selectDivExists.html()!=null ) {

            clearData();
        }


        var selList = $("[name='choose']");
        //var fileNodes = document.getElementsByClassName("fileDiv");//document.getElementsByClassName("fileDiv"); $("[mlflag='choose']");

        var isSelect = true;
        var pageScroll =  parseInt(getPageScroll());
        var pageScrollY  =  parseInt(getPageScrollY());
        var startX = (evt.x || evt.clientX) + parseInt(pageScroll);
        var startY = (evt.y || evt.clientY)+ parseInt(pageScrollY);
        var selDiv = document.createElement("div");
        selDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";
        selDiv.id = "selectDiv";
        document.body.appendChild(selDiv);

        selDiv.style.left = startX + "px";
        selDiv.style.top = startY + "px";

        var _x = null;
        var _y = null;
        clearEventBubble(evt);

        optAreaDiv.onmousemove = function() {
            evt = window.event || arguments[0];
            if (isSelect) {
                if (selDiv.style.display == "none") {
                    selDiv.style.display = "";
                }
                _x = (evt.x || evt.clientX)+pageScroll;
                _y = (evt.y || evt.clientY)+pageScrollY;
                selDiv.style.left = Math.min(_x, startX) + "px";
                selDiv.style.top = Math.min(_y, startY) + "px";
                selDiv.style.width = Math.abs(_x - startX) + "px";
                selDiv.style.height = Math.abs(_y - startY) + "px";

            }
            clearEventBubble(evt);



        };

        document.onmouseup = function () {
            isSelect = false;
            chooseArr = [];//清除以前的

            //当鼠标离开的时候计算
            // ---------------- 关键算法 --------------------- 
            var _l = selDiv.offsetLeft, _t = selDiv.offsetTop;
            var _w = selDiv.offsetWidth, _h = selDiv.offsetHeight;
            for (var i = 0; i < selList.length; i++) {
                var sl = selList[i].offsetWidth + selList[i].offsetLeft;
                var st = selList[i].offsetHeight + selList[i].offsetTop;
                if (   sl > _l  && st > _t  &&  selList[i].offsetLeft < _l + _w  && selList[i].offsetTop < _t + _h ) {
                    chooseArr.push(selList[i]);
                }
            }
            //第一个
            var fistArr = chooseArr[0];
            var x1 = 0, y1 = 0, x2 = 0, y2 = 0;
            if(chooseArr[0]!=null ){
                x1 = chooseArr[0].offsetLeft ;//该单元格距离左侧的位置
                y1 = chooseArr[0].offsetTop ;//该单元格距离上方侧的位置
                //最后一个
                x2 = chooseArr[chooseArr.length-1].offsetLeft ;//该单元格距离左侧的位置
                y2 = chooseArr[chooseArr.length-1].offsetTop ;//该单元格距离上方侧的位置
                selDiv.style.left = x1 + "px";
                selDiv.style.top = y1 + "px";
                selDiv.style.width = Math.abs(x2 - x1 + chooseArr[0].offsetWidth) + "px";
                selDiv.style.height = Math.abs(y2 - y1 + chooseArr[0].offsetHeight ) + "px";



                var bb= $("#tbgsbutton");
                if(bb==null || bb.attr("id")==null){
                    $("body").append("<img onclick='showTbgs()' src='../../ml/plug_in/images/bjbutton.png' id='tbgsbutton' " +
                        "style='position:absolute;" +
                        "top: "+y1+"px;" +
                        "left: "+(x1-25)+"px;" +
                        "width: 22px;height: 22px;z-index:1000;cursor: pointer;' />");
                }


            }else {//没有数据，删除掉选框
                var bb= $("#tbgsbutton");
                if(bb==null || bb.attr("id")==null){
                    $("#selectDiv").remove();
                }
            }

            //因为计算数组在这里计算，所以最后要判断一下
            if(chooseArr.length<=0 && clickDiv!=null){
                chooseArr.push(clickDiv);
            }



        }
    }
})();

function clearEventBubble(evt) {
    if (evt.stopPropagation)
        evt.stopPropagation();
    else
        evt.cancelBubble = true;
    if (evt.preventDefault)
        evt.preventDefault();
    else
        evt.returnValue = false;
}

